/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-27 17:44:22
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-02-17 16:52:48
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { StorePutModelContentDto, StoreFunctionsServiceProxy, StorePutServiceProxy, StoreInfoDto, StoreInfoServiceProxy, QuarantineProductDto } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
@Component({
    selector: 'app-manual-msg',
    templateUrl: './manual-msg.component.html',
    styleUrls: ['./manual-msg.component.less']
})
export class ManualMsgComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() typeStatus: string;  //查看/修改
    @Input() title: string;  //标题
    saving = false;
    active = false;
    // form = new StoreInfoDto();
    form: any;
    model = [];
    sendTime = [];
    modelOptions = [];
    sendTimeOptions = [];
    materialsNodeOptions = [];
    moduleOptions = [];
    unitOptions = [];
    materialCategoryOptions = [];
    binLocationOptions = [];
    materialProperties: Boolean = true;//0=true=正常品  ------------ 2=false=返修品
    productCode = [];// 产品编号
    nonMetallicManufacturingDate = undefined;//非金属制造日期
    nonMetallicPressingDate = undefined;//非金属压制日期
    date = undefined;//开证日期
    expirationDate = undefined;//有效期止
    apartForm = new QuarantineProductDto();
    // apartForm: any;
    isOrNot = true;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
        private _storeInfoServiceProxy: StoreInfoServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
    }
    ex() {
        this.apartForm.id = this.form.id;
        if (this.isOrNot) {
            this.apartForm.isOrNot = 1;
        } else {
            this.apartForm.isOrNot = 0;
        }
        this.apartForm.storeStockContentDto = this.form.storeStockContentDto;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._storeInfoServiceProxy.quarantineProduct(this.apartForm)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((rs) => {
                    this.saveFlag = true;
                    if (rs.code == 0) {
                        this._messageService.success('产品已隔离');
                        this.close();
                    } else {
                        this._messageService.error(rs.message);
                    }
                });
        }
    }
    save() {
        for (let i = 0; i < this.productCode.length; i++) {
            let item = this.productCode[i].code;
            if (i == 0 && this.productCode.length - 1 != 0) {
                this.form.productNumber = item + ',';
            } else if (i == 0 && this.productCode.length - 1 == 0) {
                this.form.productNumber = item;
            } else if (i == this.productCode.length - 1) {
                this.form.productNumber = this.form.productNumber + item;
            } else {
                this.form.productNumber = this.form.productNumber + item + ',';
            }
        }
        for (let i = 0; i < this.model.length; i++) {
            let item = this.model[i];
            if (i == 0 && this.model.length - 1 != 0) {
                this.form.productCode = item.value + ',';
            } else if (i == 0 && this.model.length - 1 == 0) {
                this.form.productCode = item.value;
            } else if (i == this.model.length - 1) {
                this.form.productCode = this.form.productCode + item.value;
            } else {
                this.form.productCode = this.form.productCode + item.value + ',';
            }
        }
        for (let i = 0; i < this.sendTime.length; i++) {
            let item = this.sendTime[i];
            if (i == 0 && this.sendTime.length - 1 != 0) {
                this.form.sendTime = item.value + ',';
            } else if (i == 0 && this.sendTime.length - 1 == 0) {
                this.form.sendTime = item.value;
            } else if (i == this.sendTime.length - 1) {
                this.form.sendTime = this.form.sendTime + item.value;
            } else {
                this.form.sendTime = this.form.sendTime + item.value + ',';
            }
        }
        for (const item of this.materialsNodeOptions) {
            if (item.value == this.form.materialsNodeId) {
                this.form.materialsNode = item.label;
            }
        }
        for (const item of this.materialCategoryOptions) {
            if (item.value == this.form.materialCategoryId) {
                this.form.materialCategory = item.label;
            }
        }
        for (const item of this.unitOptions) {
            if (item.value == this.form.unitId) {
                this.form.unit = item.label;
            }
        }
        for (const item of this.binLocationOptions) {
            if (item.value == this.form.binLocationId) {
                this.form.binLocation = item.label;
            }
        }
        if (this.materialProperties) {
            this.form.materialsProperty = 0;
        } else {
            this.form.materialsProperty = 1;
        }
        this.form.nonMetallicManufacturingDate = this.nonMetallicManufacturingDate;
        this.form.nonMetallicPressingDate = this.nonMetallicPressingDate;
        this.form.date = this.date;
        this.form.expirationDate = this.expirationDate;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._storeInfoServiceProxy.updateStoreInfo(this.form)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((rs) => {
                    this.saveFlag = true;
                    if (rs.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                    } else {
                        this._messageService.error(rs.message);
                    }
                });
        }
    }
    //打开弹窗
    onShown(): void {
        this.active = true;
        //单位
        this._storeFunctionsServiceProxy.getCodeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.unitOptions = [];
                this.unitOptions = result;
            });
        //型号（产品代号）
        this._storeFunctionsServiceProxy.getRocketModeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.modelOptions = [];
                this.modelOptions = result;
            });
        //物料类别
        this._storeFunctionsServiceProxy.getMaterialsTypeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialsNodeOptions = [];
                this.materialsNodeOptions = result;
            });
        //储位
        this._storeFunctionsServiceProxy.getBinLocationDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.binLocationOptions = [];
                this.binLocationOptions = result;
            });
    }
    getSendTime(event) {
        //发次
        this._storeFunctionsServiceProxy.getRocketLotNoDropList(
            this.model
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
            });
    }
    onChange(event) {
        if (this.form.materialsNodeId != undefined) {
            //物料细类
            this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
                this.form.materialsNodeId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.materialCategoryOptions = [];
                    this.materialCategoryOptions = result;
                });
        }
    }
    //查看新增任务
    show(): void {
        this.initEditInfo();
        this.initMultiSelect();
        this.active = true;
        this.modal.show();
    }
    initEditInfo() {
        //数据初始化
        this.form = this.editData;
        //规格下拉
        this._storeFunctionsServiceProxy.getMaterialsContentsModelList(
            this.form.productDrawingNo
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.moduleOptions = [];
                this.moduleOptions = result;
            });
        if (this.editData.nonMetallicManufacturingDate != null) {
            this.nonMetallicManufacturingDate = new Date(this.editData.nonMetallicManufacturingDate);
        }
        if (this.editData.nonMetallicPressingDate != null) {
            this.nonMetallicPressingDate = new Date(this.editData.nonMetallicPressingDate);
        }
        if (this.editData.date != null) {
            this.date = new Date(this.editData.date);
        }
        if (this.editData.expirationDate != null) {
            this.expirationDate = new Date(this.editData.expirationDate);
        }
        //物料细分赋值
        this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
            this.form.materialsNodeId
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialCategoryOptions = [];
                this.materialCategoryOptions = result;
                this.form.materialCategoryId = this.editData.materialCategoryId;
            });
    }
    initMultiSelect() {
        this.productCode = [];
        let productCode = this.editData.productNumber.split(',');
        for (const item of productCode) {
            let data = { code: undefined };
            if (item == 'undefined') {
                data.code = undefined;
            } else {
                data.code = item;
            }
            this.productCode.push(data);
        };
        this.model = [];
        this.sendTime = [];
        let model = this.editData.productCode.split(',');
        let modelName = this.editData.productCodeName.split(',');
        let sendTime = this.editData.sendTime.split(',');
        let sendTimeName = this.editData.sendTimeName.split(',');
        for (let i = 0; i < model.length; i++) {
            const item = model[i];
            let data = { label: undefined, value: undefined };
            data.label = modelName[i];
            data.value = item;
            this.model.push(data);
        }
        // for (const item of model) {
        //     let data = { label: undefined, value: undefined };
        //     data.label = item;
        //     data.value = item;
        //     this.model.push(data);
        // };
        let sendTimeData = [];
        // for (const item of sendTime) {
        //     let data = { label: undefined, value: undefined };
        //     data.label = item;
        //     data.value = item;
        //     sendTimeData.push(data);
        // };
        for (let i = 0; i < sendTime.length; i++) {
            const item = sendTime[i];
            let data = { label: undefined, value: undefined };
            data.label = sendTimeName[i];
            data.value = item;
            sendTimeData.push(data);
        }
        this._storeFunctionsServiceProxy.getRocketLotNoDropList(
            this.model
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
                this.sendTime = sendTimeData;
            });
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new StoreInfoDto();
        this.materialCategoryOptions = [];
        this.apartForm = new QuarantineProductDto();
        this.model = [];
        this.sendTime = [];
        this.active = false;
        this.modalSave.emit();
        this.modal.hide();
    }
}
